package com.bawei.park

import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import androidx.databinding.DataBindingUtil
import androidx.paging.PagingDataAdapter
import androidx.recyclerview.widget.DiffUtil.ItemCallback
import androidx.recyclerview.widget.RecyclerView.ViewHolder
import com.bawei.base.bean.NewsEntity
import com.bawei.park.databinding.ItemParkBinding

class ParkAdapter:PagingDataAdapter<NewsEntity,ParkAdapter.ParkViewHolder>(diff) {
    companion object{
        var diff= object :ItemCallback<NewsEntity>(){
            override fun areItemsTheSame(oldItem: NewsEntity, newItem: NewsEntity): Boolean {
                return oldItem.id==newItem.id
            }

            override fun areContentsTheSame(oldItem: NewsEntity, newItem: NewsEntity): Boolean {
                return oldItem==newItem
            }

        }
    }

    class ParkViewHolder(itemView:View):ViewHolder(itemView){
        var binding : ItemParkBinding?=DataBindingUtil.bind(itemView)
    }

    override fun onBindViewHolder(holder: ParkViewHolder, position: Int) {
        val item = getItem(position)!!
        holder.binding?.apply {
            parkTitle.text=item.title
        }
    }

    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): ParkViewHolder {
        return ParkViewHolder(
            LayoutInflater.from(parent.context).inflate( R.layout.item_park,parent,false)
        )
    }
}